package cn.hyp.bantang.module.finance.controller.admin.baselr;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;


import java.util.*;
import java.io.IOException;

import cn.hyp.bantang.framework.common.pojo.PageParam;
import cn.hyp.bantang.framework.common.pojo.PageResult;
import cn.hyp.bantang.framework.common.pojo.CommonResult;
import cn.hyp.bantang.framework.common.util.object.BeanUtils;
import static cn.hyp.bantang.framework.common.pojo.CommonResult.success;

import cn.hyp.bantang.framework.excel.core.util.ExcelUtils;

import cn.hyp.bantang.framework.apilog.core.annotation.ApiAccessLog;
import static cn.hyp.bantang.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.hyp.bantang.module.finance.controller.admin.baselr.vo.*;
import cn.hyp.bantang.module.finance.dal.dataobject.baselr.BaseLrDO;
import cn.hyp.bantang.module.finance.service.baselr.BaseLrService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 个股报告期利润")
@RestController
@RequestMapping("/finance/base-lr")
@Validated
public class BaseLrController {

    @Resource
    private BaseLrService baseLrService;

    @PostMapping("/create")
    @Operation(summary = "创建个股报告期利润")
    @PreAuthorize("@ss.hasPermission('finance:base-lr:create')")
    public CommonResult<Long> createBaseLr(@Valid @RequestBody BaseLrSaveReqVO createReqVO) {
        return success(baseLrService.createBaseLr(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新个股报告期利润")
    @PreAuthorize("@ss.hasPermission('finance:base-lr:update')")
    public CommonResult<Boolean> updateBaseLr(@Valid @RequestBody BaseLrSaveReqVO updateReqVO) {
        baseLrService.updateBaseLr(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除个股报告期利润")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('finance:base-lr:delete')")
    public CommonResult<Boolean> deleteBaseLr(@RequestParam("id") Long id) {
        baseLrService.deleteBaseLr(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得个股报告期利润")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('finance:base-lr:query')")
    public CommonResult<BaseLrRespVO> getBaseLr(@RequestParam("id") Long id) {
        BaseLrDO baseLr = baseLrService.getBaseLr(id);
        return success(BeanUtils.toBean(baseLr, BaseLrRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得个股报告期利润分页")
    @PreAuthorize("@ss.hasPermission('finance:base-lr:query')")
    public CommonResult<PageResult<BaseLrRespVO>> getBaseLrPage(@Valid BaseLrPageReqVO pageReqVO) {
        PageResult<BaseLrDO> pageResult = baseLrService.getBaseLrPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BaseLrRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出个股报告期利润 Excel")
    @PreAuthorize("@ss.hasPermission('finance:base-lr:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBaseLrExcel(@Valid BaseLrPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BaseLrDO> list = baseLrService.getBaseLrPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "个股报告期利润.xls", "数据", BaseLrRespVO.class,
                        BeanUtils.toBean(list, BaseLrRespVO.class));
    }

}